package com.chen.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;

/**
 * ClassName: Cacluator
 * Package: com.chen.example
 * Description: 计算机界面设计
 *
 * @Author 陈俊辉
 * @Create 2024/4/20 5:47
 * @Version 1.0
 */
public class Cacluator extends JFrame {
    //用于判断是否重新开始
    private boolean start = true;
    private double result = 0;
    //存放加减乘除等于等
    private String command = "=";
    private JTextField jTextField;
    private JPanel jPanel = new JPanel();
    private JButton[] jButtons;

    //用构造方法进行必要的设置
    public Cacluator() {

        this.setTitle("科学计算器");
        this.setSize(600, 300);
        this.setLocationRelativeTo(null);

        //添加文本域
        jTextField = new JTextField(30);
        jTextField.setText("");
        jTextField.setEditable(true);
        this.add(jTextField, "North");

        //添加按钮
        jPanel.setLayout(new GridLayout(5, 7, 3, 3));
        String name[] = {
                "+", "-", "*", "/", "C", "Back", "=", "sin", "cos", "tan", "cot", "7", "8", "9",
                "to 2", "to 8", "author", "1/X", "4", "5", "6", "2 to 10", "8 to 10", "X!", "%",
                "1", "2", "3", "X^2", "X^Y", "2|X", "3|X", "TIME", "0", "."
        };
        jButtons = new JButton[name.length];
        MyActionListener1 actionListener = new MyActionListener1(jTextField);

        //利用循环创建按钮对象并添加事件监听器
        for (int i = 0; i < name.length; i++) {

            jButtons[i] = new JButton(name[i]);
            //设置的是同一个事件监视器
            jButtons[i].addActionListener(actionListener);

            //设置按钮背景颜色
            jButtons[i].setBackground(Color.lightGray);
            if (name[i].equals("="))
                jButtons[i].setBackground(Color.RED);
            else if ((int) name[i].charAt(0) >= 48 && (int) name[i].charAt(0) <= 57
                    && name[i].length() == 1)
                jButtons[i].setBackground(Color.WHITE);
            else if (name[i].equals("back"))
                jButtons[i].setBackground(Color.GRAY);

            jPanel.add(jButtons[i]);
        }

        this.add(jPanel);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);

    }
    public static void main(String[] args) {
        Cacluator Calculator = new Cacluator();

    }
}
